<template>
  <el-dialog
    :title="'码单'"
    v-model="visible"
    draggable
    :width="968"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-tabs type="border-card">
      <el-form
        :model="form"
        :rules="rules"
        ref="dialogForm"
        label-width="60px"
        label-position="right"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品" prop="material.name">
              <el-input v-model="form.material.name" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="颜色" prop="color_name">
              <el-input v-model="form.color_name" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工艺" prop="process_route">
              <el-input v-model="form.process_route" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="缸号" prop="dyelot">
              <el-input v-model="form.dyelot" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="挂码%" prop="hanging_code">
              <el-input v-model="form.hanging_code"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="检验机" prop="checkmac_id">
              <skAutoChoose
                v-model="form.checkmac.name"
                :name="'loom'"
                :assign="form"
                :asKey="'checkmac_id'"
                :sort="'dosage'"
              >
              </skAutoChoose>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
            <el-form-item label="卷号" prop="reel">
              <el-input v-model="form.reel"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item label="匹数" prop="auxiliary_quantity">
              <el-input v-model="form.auxiliary_quantity"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item label="数量" prop="quantity">
              <el-input v-model="form.quantity" @keyup.enter="submit"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="单位" prop="unit_id">
              <skDicSelect v-model="form.unit_id" :code="'unitMM'"> </skDicSelect>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="等级" prop="grade">
              <skDicSelect
                v-model="form.grade"
                :code="'DefectGrade'"
                :placeholder="'疵点等级'"
              >
              </skDicSelect>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="检验员" prop="swipor_id">
              <skAutoChoose
                v-model="form.swipor.name"
                :name="'employee'"
                :asKey="'swipor_id'"
                :assign="form"
              >
              </skAutoChoose>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <sc-VxeTable
              ref="xGrid"
              :gridOps="{ height: 426 }"
              :gridEvts="gridEvents"
              :tablecode="'pro_process_inspect_pack'"
              :url="$API.pro.inspectPack.list"
              :params="getParams"
            >
            </sc-VxeTable>
          </el-col>
        </el-row>
      </el-form>
    </el-tabs>
  </el-dialog>
</template>

<script>
import skAutoChoose from "@/components/skAutoChoose";
import skDicSelect from "@/components/skDicSelect";
import tool from "@/utils/tool";
import XEUtils from "xe-utils";
export default {
  emits: ["success", "closed"],
  components: { skAutoChoose, skDicSelect },
  data() {
    return {
      form: {
        material: {},
        swipor: {},
        checkmac: {},
        hanging_code: 100,
        grade: "A",
        style_id: 0,
      },
      rules: {
        quantity: [{ required: true, message: "产量不能为空！" }],
      },
      gridEvents: {
        add: () => {
          this.submit();
        },
      },
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {},
  methods: {
    //显示
    open() {
      this.visible = true;
      return this;
    },
    getParams() {
      return { inspect_id: this.form.id };
    },
    //表单提交方法
    submit() {
      if (this.isSaveing) return;
      this.$refs.dialogForm.validate(async valid => {
        if (valid) {
          this.isSaveing = true;
          this.$API.pro.inspectPack.save
            .post({
              inspect_id: this.form.id,
              reel: this.form.reel,
              auxiliary_quantity: this.form.auxiliary_quantity,
              quantity: this.form.quantity,
              fixed_auxiliary_quantity: this.form.fixed_auxiliary_quantity,
              checkmac_id: this.form.checkmac_id,
              hanging_code: this.form.hanging_code,
              remark: this.form.remark,
              swipor_id: this.form.swipor_id,
              unit_id: this.form.unit_id,
              grade: this.form.grade,
              style_id: this.form.style_id,
              location_id: "3255",
            })
            .then(res => {
              if (res.code == 200) {
                this.$emit("success");
                this.$refs.xGrid.refresh();
                this.$message.success("操作成功");
                this.form.quantity = "";
                this.form.reel = XEUtils.toNumber(this.form.reel) + 1;
              }
            })
            .finally(() => {
              this.isSaveing = false;
            });
        }
      });
    },

    //表单注入数据
    setData(data) {
      Object.assign(this.form, tool.removeNull(data));
      this.form.reel = XEUtils.toNumber(data.reel) + 1;
      this.form.auxiliary_quantity = 1;
      this.form.quantity = "";
      return this;
    },
  },
};
</script>

<style></style>
